<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryRef"
      :inline="true"
      label-width="100px"
    >
      <el-form-item class="mr" label="故障ID" prop="faultId">
        <el-input
          v-model="queryParams.faultId"
          placeholder="请输入"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item class="mr" label="设备ID" prop="equipmentId">
        <el-input
          v-model="queryParams.equipmentId"
          placeholder="请输入"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item class="mr" label="故障类型" prop="faultType">
        <el-select
          v-model="queryParams.faultType"
          placeholder="请选择"
          clearable
          style="width: 168px"
        >
          <el-option
            v-for="{ label, value } in sys_device_failure_type"
            :key="value"
            :label="label"
            :value="value"
          />
        </el-select>
      </el-form-item>
      <el-form-item class="mr" label="发现人ID" prop="findPersonId">
        <el-input
          v-model="queryParams.findPersonId"
          placeholder="请输入"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item class="mr" label="巡检记录ID" prop="inspectionRecordsId">
        <el-input
          v-model="queryParams.inspectionRecordsId"
          placeholder="请输入"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item class="mr" label="维修方法" prop="fixMethod">
        <el-select
          v-model="queryParams.fixMethod"
          placeholder="请选择"
          clearable
          style="width: 168px"
        >
          <el-option
            v-for="{ label, value } in sys_equipment_method"
            :key="value"
            :label="label"
            :value="value"
          />
        </el-select>
      </el-form-item>
      <el-form-item class="mr" label="发现时间" prop="findTime">
        <el-date-picker
          v-model="queryParams.findTime"
          format="YYYY-MM-DD"
          value-format="YYYY-MM-DD"
          type="date"
          style="width: 168px"
        />
      </el-form-item>
      <el-form-item class="mr" label="维修时间" prop="fixTime">
        <el-date-picker
          v-model="queryParams.fixTime"
          format="YYYY-MM-DD"
          value-format="YYYY-MM-DD"
          type="date"
          style="width: 168px"
        />
      </el-form-item>
      <el-form-item style="float: right; margin-right: 100px">
        <el-button
          type="primary"
          icon="Search"
          @click="handleQuery"
          v-hasPermi="['operation:device:query']"
          >搜索</el-button
        >
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['operation:device:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['operation:device:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['operation:device:remove']"
          >删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['operation:device:export']"
          >导出</el-button
        >
      </el-col>
    </el-row>

    <el-table
      v-loading="loading"
      :data="deviceList"
      :row-class-name="tableRowClassName"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="50" align="center" />
      <el-table-column label="序号" align="center" type="index" />
      <el-table-column label="故障ID" align="center" prop="faultId">
        <template #default="{ row }"> No.{{ row.faultId }} </template>
      </el-table-column>
      <el-table-column label="设备ID" align="center" prop="equipmentId" />
      <el-table-column label="故障类型" align="center" prop="faultType">
        <template #default="{ row }">
          <dict-tag :options="sys_device_failure_type" :value="row.faultType" />
        </template>
      </el-table-column>
      <el-table-column
        label="故障描述"
        align="center"
        prop="faultDescription"
      />
      <el-table-column label="故障附件" align="center" prop="faultAttachment" />
      <el-table-column
        label="发现时间"
        width="100"
        align="center"
        prop="findTime"
      />
      <el-table-column label="发现人ID" align="center" prop="findPersonId" />
      <el-table-column label="维修方法" align="center" prop="fixMethod">
        <template #default="{ row }">
          <dict-tag :options="sys_equipment_method" :value="row.fixMethod" />
        </template>
      </el-table-column>
      <el-table-column label="维修附件" align="center" prop="fixAttachment" />
      <el-table-column
        label="维修时间"
        width="100"
        align="center"
        prop="fixTime"
      />
      <el-table-column label="维修人ID" align="center" prop="fixPersonId" />
      <el-table-column
        label="关联巡检记录ID"
        align="center"
        prop="inspectionRecordsId"
      />
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding  fixed-width"
      >
        <template #default="{ row }">
          <el-button
            type="text"
            icon="View"
            @click="handleView(row)"
            v-hasPermi="['operation:device:detail']"
            >详细</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改设备故障信息对话框 -->
    <el-dialog :title="title" v-model="open" width="600px" append-to-body>
      <el-form :model="form" :rules="rules" ref="deviceRef" label-width="90px">
        <el-row>
          <el-col :span="24">
            <el-form-item label="故障ID" prop="faultId">
              <el-input v-model="form.faultId" placeholder="请输入" clearable />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="设备ID" prop="equipmentId">
              <el-input
                v-model="form.equipmentId"
                placeholder="请输入"
                clearable
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="故障类型" prop="faultType">
              <el-select
                v-model="form.faultType"
                placeholder="请选择"
                clearable
              >
                <el-option
                  v-for="{ label, value } in sys_device_failure_type"
                  :key="value"
                  :label="label"
                  :value="value"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="故障描述" prop="faultDescription">
              <el-input
                type="textarea"
                :rows="2"
                placeholder="请输入内容"
                v-model="form.faultDescription"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="故障附件" prop="faultAttachment">
              <el-input v-model="form.faultAttachment" placeholder="请输入" />
            </el-form-item>
            <el-form-item label="发现时间" prop="findTime">
              <el-date-picker
                v-model="form.findTime"
                format="YYYY-MM-DD HH:mm"
                value-format="YYYY-MM-DD HH:mm"
                type="datetime"
              />
            </el-form-item>
            <el-form-item label="发现人ID" prop="findPersonId">
              <el-input
                v-model="form.findPersonId"
                placeholder="请输入"
                clearable
              />
            </el-form-item>
            <el-form-item label="维修方法" prop="fixMethod">
              <el-select
                v-model="form.fixMethod"
                placeholder="请选择"
                clearable
              >
                <el-option
                  v-for="{ label, value } in sys_equipment_method"
                  :key="value"
                  :label="label"
                  :value="value"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="维修附件" prop="fixAttachment">
              <el-input
                v-model="form.fixAttachment"
                placeholder="请输入"
                clearable
              />
            </el-form-item>
            <el-form-item label="维修时间" prop="fixTime">
              <el-date-picker
                v-model="form.fixTime"
                format="YYYY-MM-DD HH:mm"
                value-format="YYYY-MM-DD HH:mm"
                type="datetime"
              />
            </el-form-item>
            <el-form-item label="维修人ID" prop="fixPersonId">
              <el-input
                v-model="form.fixPersonId"
                placeholder="请输入"
                clearable
              />
            </el-form-item>
            <el-form-item label="巡检记录ID" prop="inspectionRecordsId">
              <el-input
                v-model="form.inspectionRecordsId"
                placeholder="请输入"
                clearable
              />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>

    <!-- 设备故障详细 -->
    <el-dialog
      title="设备故障详细"
      v-model="openView"
      width="600px"
      append-to-body
    >
      <el-form :model="form" label-width="130px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="故障ID：">{{
              'No.' + form.faultId
            }}</el-form-item>
            <el-form-item label="故障类型：">
              <dict-tag
                :options="sys_device_failure_type"
                :value="form.faultType"
              />
            </el-form-item>
            <el-form-item label="故障描述：">
              {{ form.faultDescription }}
            </el-form-item>
            <el-form-item label="故障附件：">
              {{ form.faultAttachment }}
            </el-form-item>
            <el-form-item label="发现人ID：">
              {{ form.findPersonId }}
            </el-form-item>
            <el-form-item label="发现时间：">
              {{ form.findTime }}
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="设备ID：">{{ form.equipmentId }}</el-form-item>
            <el-form-item label="关联巡检记录ID：">
              {{ form.inspectionRecordsId }}
            </el-form-item>
            <el-form-item label="维修方法：">
              <dict-tag
                :options="sys_equipment_method"
                :value="form.fixMethod"
              />
            </el-form-item>
            <el-form-item label="维修附件：">
              {{ form.fixAttachment }}
            </el-form-item>
            <el-form-item label="维修人ID：">
              {{ form.fixPersonId }}
            </el-form-item>
            <el-form-item label="维修时间：">
              {{ form.fixTime }}
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="openView = false">关 闭</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>
<script setup name="Device">
import {
  listDevice,
  getDevice,
  delDevice,
  addDevice,
  updateDevice
} from '@/api/operation/device'

const { proxy } = getCurrentInstance()

/* 获取单位字典 */
const { sys_device_failure_type } = proxy.useDict('sys_device_failure_type') //故障类型
const { sys_equipment_method } = proxy.useDict('sys_equipment_method') //维修方法

const deviceList = ref([])
const open = ref(false)
const loading = ref(true)
const sns = ref([])
const ids = ref([])
const single = ref(true)
const multiple = ref(true)
const openView = ref(false)
const title = ref('')
const total = ref(0)
const dateRange = ref([])

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    id: undefined,
    equipmentId: undefined,
    faultType: undefined,
    findPersonId: undefined,
    inspectionRecordsId: undefined,
    findTime: undefined,
    fixTime: undefined
  },
  rules: {
    faultId: [{ required: true, message: '故障ID不能为空', trigger: 'blur' }],
    equipmentId: [
      { required: true, message: '设备ID不能为空', trigger: 'blur' }
    ]
  }
})

const { queryParams, form, rules } = toRefs(data)

/** 查询养护内容列表 */
function getList() {
  loading.value = true
  listDevice(queryParams.value).then((response) => {
    deviceList.value = response.rows
    total.value = response.total
    loading.value = false
  })
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1
  getList()
}

/** 重置按钮操作 */
function resetQuery() {
  dateRange.value = []
  proxy.resetForm('queryRef')
  handleQuery()
}

/* 重置新增和修改表单 */
function reset() {
  form.value = {}
  proxy.resetForm('deviceRef')
}

/** 新增按钮操作 */
function handleAdd() {
  reset()
  open.value = true
  title.value = '添加设备故障信息'
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset()
  const deviceId = row.id || ids.value
  getDevice(deviceId).then((response) => {
    form.value = response.data
    open.value = true
    title.value = '修改设备故障信息'
  })
}

/** 删除按钮操作 */
function handleDelete(row) {
  const deviceIds = row.id || ids.value
  proxy.$modal
    .confirm('是否确认删除序号为："' + sns.value + '"的数据项?')
    .then(function () {
      return delDevice(deviceIds)
    })
    .then(() => {
      getList()
      proxy.$modal.msgSuccess('删除成功')
    })
    .catch(() => {})
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download(
    'operations/device/export',
    {
      ...queryParams.value
    },
    `device_${new Date().getTime()}.xlsx`
  )
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs['deviceRef'].validate((valid) => {
    if (valid) {
      if (form.value.id != undefined) {
        updateDevice(form.value).then((response) => {
          proxy.$modal.msgSuccess('修改成功')
          open.value = false
          getList()
        })
      } else {
        addDevice(form.value).then((response) => {
          proxy.$modal.msgSuccess('新增成功')
          open.value = false
          getList()
        })
      }
    }
  })
}

/** 取消按钮 */
function cancel() {
  open.value = false
  reset()
}

/** 把每一行的索引放进row */
function tableRowClassName({ row, rowIndex }) {
  row.index = rowIndex + 1
}

/** 多选框选中数据 */
function handleSelectionChange(selection) {
  sns.value = selection
    .map((item) => +item.index)
    .sort(function (a, b) {
      return a - b
    })
  ids.value = selection.map((item) => item.id)
  single.value = selection.length != 1
  multiple.value = !selection.length
}

/** 详细按钮操作 */
function handleView(row) {
  openView.value = true
  form.value = row
}

// 初始化列表
getList()
</script>
<style lang="scss" scoped>
.mr {
  margin-right: 5px;
}
</style>
